package sfeir.ergosum;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class PreferencesLoginPass extends DialogPreference {

	private String mLogin, mPass;
	private TextView loginView, passView;
	
	//private On closelistener = null;

	public PreferencesLoginPass(Context context, AttributeSet attrs) {
		super(context, attrs);
		mLogin = new String();
		mPass = new String();
		setDialogLayoutResource(R.layout.preferences_loginpass);
		setPersistent(true);
		
	}

	@Override
	protected View onCreateDialogView() {
		return super.onCreateDialogView();
	}

	@Override
	protected void onBindDialogView(View view) {
		super.onBindView(view);

		// Set our custom views inside the layout
		loginView = (TextView) view.findViewById(R.id.preferences_loginpass_log_field);
		passView = (TextView) view.findViewById(R.id.preferences_loginpass_pass_field);

		if (loginView != null) {
			loginView.setText(mLogin);
		}
		if (passView != null) {
			passView.setText(mPass);
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {

		switch (which) {
		case DialogInterface.BUTTON1:
			String newLogin = (String) loginView.getText().toString();
			String newPass = (String) passView.getText().toString();

			mLogin = newLogin;
			mPass = newPass;

			persistString(mLogin + "\n" + mPass);

			notifyChanged();
			break;
		case DialogInterface.BUTTON2:
			return;
		}

	}

	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		// This preference type's value type is Integer, so we read the default
		// value from the attributes as an Integer.
		// return a.getInteger(index, 0);
		return a.getString(index);
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
		if (restoreValue) {
			// Restore state
			// mClickCounter = getPersistedInt(mClickCounter);
			String[] values;
			values = getPersistedString(mLogin).split("\n");
			mLogin = values[0];
			if(values.length==2)
				mPass = values[1];
		} else {
			// Set state
			String value = (String) defaultValue;
			mLogin = value;
			persistString(value);

			// int value = (Integer) defaultValue;
			// mClickCounter = value;
			// persistInt(value);
		}
	}

	@Override
	protected Parcelable onSaveInstanceState() {

		final Parcelable superState = super.onSaveInstanceState();
		if (isPersistent()) {
			// No need to save instance state since it's persistent
			return superState;
		}

		// Save the instance state
		final SavedState myState = new SavedState(superState);
		// myState.clickCounter = mClickCounter;
		myState.login = mLogin;
		myState.pass = mPass;

		return myState;
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		if (!state.getClass().equals(SavedState.class)) {
			// Didn't save state for us in onSaveInstanceState
			super.onRestoreInstanceState(state);
			return;
		}

		// Restore the instance state
		SavedState myState = (SavedState) state;
		super.onRestoreInstanceState(myState.getSuperState());
		mLogin = myState.login;
		mPass = myState.pass;
		notifyChanged();
	}
	
	public void openDialog() {
	    showDialog(null);
	}

	private static class SavedState extends BaseSavedState {
		String login, pass;

		public SavedState(Parcel source) {
			super(source);
			login = source.readString();
			pass = source.readString();
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			super.writeToParcel(dest, flags);
			dest.writeString(login);
			dest.writeString(pass);
		}

		public SavedState(Parcelable superState) {
			super(superState);
		}

		public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
			public SavedState createFromParcel(Parcel in) {
				return new SavedState(in);
			}

			public SavedState[] newArray(int size) {
				return new SavedState[size];
			}
		};
	}

}
